package com.openapi.protocol.decode.subPackageHandle.mediaFileList;

import com.openapi.data.JT808PackageData;
import com.openapi.data.JT808PushData;
import com.openapi.protocol.decode.subPackageHandle.SubPackageHandle;
import com.openapi.protocol.decode.subPackageHandle.SubPackageStatus;
import exsun.jt808.data.config.jt808.Jt808ReceiveMsgTopic;
import exsun.jt808.data.upload_data.jt1078.MediaFileList;
import exsun.jt808.data.upload_data.jt1078.MediaListItem;
import exsun.jt808.data.util.parse.DecoderUtils;
import exsun.jt808.data.util.parse.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 视频文件资源列表
 */
@Component
public class MediaFileList_0x1205 extends SubPackageHandle<JT808PackageData>
{
    private static final Logger logger = LoggerFactory.getLogger(MediaFileList_0x1205.class);

    public JT808PushData decode(JT808PackageData data){
        try {
            SubPackageStatus status = super.append(data);
            String key = getPakKey(data);
            if(status == SubPackageStatus.complete){
                byte[] totalBytes = super.getAllData(data);
                MediaFileList fileList = parse(data,  totalBytes);
                super.kafkaService.push(fileList);
                super.remove(key);
                logger.info("音视频资源列表接收完成！{}", data.dvo);
            }
            return super.Response(data);
        }catch (Exception e){
            logger.error("音视频资源列表处理异常:{}",e.getMessage());
        }
        return null;
    }

    @Override
    public String getPakKey(JT808PackageData data) {
        return data.dvo + "-" + data.getPackageSize();
    }

    @Override
    public MediaFileList parse(JT808PackageData data, byte[] totalBodys) {
        MediaFileList obj = new MediaFileList();
        super.setResultObj(obj, data, Jt808ReceiveMsgTopic.MediaFileList);
        obj.setOrderSerialNo(DecoderUtils.parseIntFromBytes(totalBodys, 0, 2));
        int count = DecoderUtils.parseIntFromBytes(totalBodys, 2, 4);
        int index = 6;
        for(int n = 0;n<count;n++){
            MediaListItem item  = new MediaListItem();
            item.setChanelId(totalBodys[index]);
            index+=1;
            item.setStartTime(TimeUtil.getDateByFormat(DecoderUtils.parseBcdStringFromBytes(totalBodys, index, 6), TimeUtil.dateFormatyyMMddHHmmss));
            index+=6;
            item.setEndTime(TimeUtil.getDateByFormat(DecoderUtils.parseBcdStringFromBytes(totalBodys, index, 6), TimeUtil.dateFormatyyMMddHHmmss));
            index+=6;
            item.setAlarmHexStr(DecoderUtils.parseBcdStringFromBytes(totalBodys, index, 8));
            index+=8;
            item.setMediaType(totalBodys[index]);
            index+=1;
            item.setCodeType(totalBodys[index]);
            index+=1;
            item.setStorageType(totalBodys[index]);
            index+=1;
            item.setFileSize(DecoderUtils.parseIntFromBytes(totalBodys, 2, 4));
            obj.getMediaListItems().add(item);
            index+=4;
        }
        return obj;
    }
}

